<template>
  <div class="register">
    <template v-if="phoneRequired == false && emailRequired == false">
      <RegisterCommon
        ref="RegisterCommon"
        :registerForm="registerForm"
        :btnControl="btnControl"
        :captchaEnabled="captchaEnabled"
        :codeUrl="codeUrl"
        @getCode="getCode"
        @handleRegister="handleRegister" />
    </template>
    <template v-else>
      <el-tabs v-model="registerType" class="register-tabs" :before-leave="tabClick">
        <el-tab-pane label="手机号注册" name="phone" v-if="phoneRequired">
          <RegisterCommon
            ref="RegisterCommonPhone"
            :registerForm="registerForm"
            :btnControl="btnControl"
            :captchaEnabled="captchaEnabled"
            :codeUrl="codeUrl"
            @getCode="getCode"
            @handleRegister="handleRegister">
            <template v-slot:phoneCode>
              <el-form-item label="" prop="phoneNumber">
                <el-input
                  v-model="registerForm.phoneNumber"
                  prefix-icon="el-icon-mobile-phone"
                  autocomplete="off"
                  placeholder="请输入手机号" />
              </el-form-item>
              <el-form-item label="" prop="authCode">
                <el-input
                  v-model="registerForm.authCode"
                  autocomplete="off"
                  class="code-input"
                  placeholder="请输入手机验证码">
                  <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                </el-input>
                <el-button
                  :disabled="btnControl.phoneValidateCodeBtn"
                  class="code-btn"
                  type="primary"
                  size="small"
                  @click="sendPhoneCodeHandle">
                  {{ btnControl.phoneValidateCodeBtnText }}
                </el-button>
              </el-form-item>
            </template>
          </RegisterCommon>
        </el-tab-pane>
        <el-tab-pane label="邮箱注册" name="email" v-if="emailRequired">
          <RegisterCommon
            ref="RegisterCommonEmail"
            :registerForm="registerForm"
            :btnControl="btnControl"
            :captchaEnabled="captchaEnabled"
            :codeUrl="codeUrl"
            @getCode="getCode"
            @handleRegister="handleRegister">
            <template v-slot:emailCode>
              <el-form-item label="" prop="email">
                <el-input v-model="registerForm.email" autocomplete="off" placeholder="请输入邮箱">
                  <svg-icon slot="prefix" icon-class="email" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item label="" prop="authCode">
                <el-input
                  v-model="registerForm.authCode"
                  autocomplete="off"
                  class="code-input"
                  placeholder="请输入邮箱验证码">
                  <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                </el-input>
                <el-button
                  :disabled="btnControl.emailValidateCodeBtn"
                  class="code-btn"
                  type="primary"
                  size="small"
                  @click="sendEmailCodeHandle">
                  {{ btnControl.emailValidateCodeBtnText }}
                </el-button>
              </el-form-item>
            </template>
          </RegisterCommon>
        </el-tab-pane>
      </el-tabs>
    </template>

    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2022 LiZeYi All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import RegisterCommon from "./RegisterCommon.vue"
import { getCodeImg, register } from "@/api/login"

export default {
  name: "Register",
  components: {
    RegisterCommon,
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"))
      } else {
        callback()
      }
    }
    return {
      registerType: "phone",
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: "",
        phoneNumber: "",
        authCode: "",
        email: "",
      },
      btnControl: {
        phoneValidateCodeBtn: false,
        phoneValidateCodeBtnText: "发送验证码",
        emailValidateCodeBtn: false,
        emailValidateCodeBtnText: "发送验证码",
      },
      captchaEnabled: true,
      phoneRequired: false,
      emailRequired: false,
      codeUrl: "",
      runOnce: true,
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    tabClick() {
      this.registerForm.code = ""
      this.registerForm.authCode = ""
      this.getCode()
      return true
    },
    setTabShow() {
      if (this.runOnce) {
        if (this.phoneRequired) {
          this.registerType = "phone"
        } else if (this.emailRequired) {
          this.registerType = "email"
        }
        this.runOnce = false
      }
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        this.phoneRequired = res.phoneRequired === undefined ? false : res.phoneRequired
        this.emailRequired = res.emailRequired === undefined ? false : res.emailRequired
        this.setTabShow()
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.registerForm.uuid = res.uuid
        }
      })
    },
    sendPhoneCodeHandle() {
      this.$refs.RegisterCommonPhone.sendAuthCode("phone")
    },
    sendEmailCodeHandle() {
      this.$refs.RegisterCommonEmail.sendAuthCode("email")
    },
    handleRegister() {
      register(this.registerForm, { registerType: this.registerType })
        .then((res) => {
          const username = this.registerForm.username
          this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", "系统提示", {
            dangerouslyUseHTMLString: true,
            type: "success",
          })
            .then(() => {
              this.$router.push("/login")
            })
            .catch(() => {})
        })
        .catch(() => {
          this.loading = false
          if (this.captchaEnabled) {
            this.getCode()
          }
        })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.register-tabs {
  padding: 20px;
  border-radius: 6px;
  background: #ffffff;
}

.register-form {
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}

.code-input {
  width: 180px;
}
.code-btn {
  margin-left: 20px;
  border-radius: 10px;
  width: 145px;
}
</style>
